package fans_group

import (
	"context"
	"github.com/godruoyi/go-snowflake"
	"longmen/server/internal/rpc/tencentim/fans_group/fans_messages"
	"longmen/server/internal/rpc/tencentim/imsdk"
	messages2 "longmen/server/internal/rpc/tencentim/messages"
	"longmen/server/pkg/common/api_models"
	"longmen/server/pkg/db/redis/cache"
	"time"
)

type CustomReply struct {
	Uid         int                       `json:"uid"`          // 后台模仿谁发消息
	GroupId     string                    `json:"group_id"`     // 群ID
	NickName    string                    `json:"nick_name"`    // 昵称
	Avatar      string                    `json:"avatar"`       // 头像
	Content     string                    `json:"content"`      // 内容
	Type        fans_messages.MsgSendType `json:"type"`         // 消息类型 0-普通文本消息 1-加入提示 2-撤回 3-删除 4-被踢出 5-被禁言 6-解禁 7-图文消息
	SendTime    int64                     `json:"send_time"`    // 发送时间
	Role        int                       `json:"role"`         // 角色 0-群员 1-管理 2-群主 3-机器人
	ImageUrl    string                    `json:"image_url"`    // 图文消息的图片链接
	ImageWidth  int                       `json:"image_width"`  // 图文消息的图片宽度
	ImageHeight int                       `json:"image_height"` // 图文消息的图片高度
}

func init() {
	snowflake.SetStartTime(time.Date(2021, 9, 1, 0, 0, 0, 0, time.UTC))
	snowflake.SetMachineID(1)
}

// SendToGroup 后台发送消息
func (c CustomReply) SendToGroup(ctx context.Context) (FansGroupHistory, error) {
	eContent, _ := cache.AesEncrypt(c.Content)
	history := FansGroupHistory{
		Seq:         int64(snowflake.ID()),
		Uid:         c.Uid,
		GroupId:     c.GroupId,
		NickName:    c.NickName,
		Avatar:      c.Avatar,
		Content:     c.Content,
		SendTime:    time.Now().Unix(),
		Type:        c.Type,
		Role:        c.Role,
		ImageUrl:    c.ImageUrl,
		ImageWidth:  c.ImageWidth,
		ImageHeight: c.ImageHeight,
	}
	if c.SendTime > 0 {
		history.SendTime = c.SendTime
	}
	err := history.writeToEsAndMQ(ctx)
	if err != nil {
		return history, err
	}

	history.Content = eContent

	resp := fans_messages.SayRepMessage{
		ReplyMessage: messages2.ReplyMessage{Type: messages2.SayReplyMessageType},
		Resp: fans_messages.SayRespMessage{
			Seq:         history.Seq,
			Uid:         history.Uid,
			GroupId:     history.GroupId,
			NickName:    history.NickName,
			Avatar:      history.Avatar,
			Content:     fans_messages.SayContent(history.Content),
			SendTime:    history.SendTime,
			Type:        history.Type,
			Role:        history.Role,
			ImageUrl:    history.ImageUrl,
			ImageWidth:  history.ImageWidth,
			ImageHeight: history.ImageHeight,
		},
	}
	//写入最近消息
	cache.SetGroupChatLatestMsg(history.GroupId, &api_models.GroupChatLatestMsg{
		FansGroupId:   history.GroupId,
		LatestMsgTime: time.Now().Unix(),
		LatestMsg: func() string {
			if len(history.ImageUrl) > 0 {
				return "[图片]"
			}
			return c.Content
		}(),
		FromUserId:       history.Uid,
		FromUserNickname: history.NickName,
	})
	return history, imsdk.GetSdk().BroadcastInterfaceGroupString(c.GroupId, resp, 0)
}

func SendToIm(ctx context.Context, history *FansGroupHistory) error {
	eContent, _ := cache.AesEncrypt(history.Content)
	history.Content = eContent
	resp := fans_messages.SayRepMessage{
		ReplyMessage: messages2.ReplyMessage{Type: messages2.SayReplyMessageType},
		Resp: fans_messages.SayRespMessage{
			Seq:      history.Seq,
			Uid:      history.Uid,
			GroupId:  history.GroupId,
			NickName: history.NickName,
			Avatar:   history.Avatar,
			Content:  fans_messages.SayContent(history.Content),
			SendTime: history.SendTime,
			Type:     history.Type,
			Role:     history.Role,
		},
	}

	return imsdk.GetSdk().BroadcastInterfaceGroupString(history.GroupId, resp, 0)
}
